--#include "data\config\misc\MeritoriousConfig.lua" once	-功勋消耗配置
--#include "data\functions\Activity\Common\Consumes.lua" --消耗 通用
--#include "data\functions\Consume.lua" once
--在用，2017.1


--功勋处理
MiscsImplement.MeritoriousHandle = function( sysarg, msgid, packet )
	local var = Actor.getStaticVar(sysarg)

	local Reinlevel = Actor.getIntProperty(sysarg, PROP_ACTOR_MERITORIOUS_LEVEL)
	
	local optResult = MeritoriousHandleImpl(sysarg, Reinlevel + 1)

	local npack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sMeritoriousErrorCode)  --申请一个数据包
	DataPack.writeByte(npack, optResult)
	DataPack.flush(npack);
	
	if optResult == 0 then -- 成功
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.Meritorious003, ttFlyTip)
		return
	end
end

function MeritoriousHandleImpl(sysarg, nMeritorious)
	local config = MeritoriousCfg.MeritoriousConsumes[nMeritorious]
	if not config then
		System.trace("Can not find MeritoriousConsumes config, nMeritorious="..nMeritorious)
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.Meritorious004,ttFlyTip)
		return 1	-- 配置错误
	end
	
	--条件检测
	local bCheck, nReqValue = CheckMeritoriousCondition(sysarg, config)
	if bCheck ~= true then
		return 2	-- 条件检测不符号
	end
	
	local  bResult = 0
	
	local currentValue = Actor.getIntProperty(sysarg,PROP_ACTOR_MERITORIOUS_VALUE)
	--local currentLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, MeritoriousCfg.activeLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0055, MeritoriousCfg.activeLevel),ttFlyTip)
		return 2 -- 条件检测不符号
	end

	Actor.setIntProperty(sysarg,PROP_ACTOR_MERITORIOUS_VALUE,currentValue - nReqValue)
	
	
	Actor.SetActorMeritorious(sysarg, nMeritorious)

	Actor.triggerAchieveEvent(sysarg, 102, nMeritorious)
	return 0
end

--功勋条件检测
function CheckMeritoriousCondition(sysarg, config)

	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if config.reqLevel and not Actor.checkActorLevel(sysarg, config.reqLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.Meritorious001, config.reqLevel), ttFlyTip)
		return false, 0
	end	
		
	local reqSoul = 0
	for k, v in ipairs(config.consumes) do
		if v.type == qatMeritorious then 
			reqSoul = v.count
		end
	end

	local cSoul = Actor.getIntProperty(sysarg, PROP_ACTOR_MERITORIOUS_VALUE)
	if cSoul < reqSoul then
		Actor.sendItemLack( sysarg, qatMeritorious, 0, reqSoul )
		--Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.Meritorious002, reqSoul), ttFlyTip)
		return false, reqSoul
	end
	
	return true, reqSoul
end

MiscsImplement.MeritoriousDailyHandle = function( sysarg, msgid, packet )
	SendDailyMeritorious(sysarg)
end

function SendDailyMeritorious(sysarg)
	local npack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sDailyMeritorious)  --申请一个数据包
	local var = Actor.getStaticVar(sysarg)
	if not var.days then 
		var.days = {} 
	end
	if not var.days.dailyMeritorious  then 
		var.days.dailyMeritorious = 0 
	end
	DataPack.writeInt(npack, var.days.dailyMeritorious)
	DataPack.flush(npack);
end

EventCallDispatcher.DailyMeritoriousChange = function (sysarg, args)
	if args[1] == nil then 
 		return 
 	end   
 	local var = Actor.getStaticVar(sysarg)
 	if not var.days then 
		var.days = {} 
	end
	if not var.days.dailyMeritorious  then 
		var.days.dailyMeritorious = 0 
	end
 	var.days.dailyMeritorious = var.days.dailyMeritorious + args[1]

 	SendDailyMeritorious(sysarg)
end

MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cMeritoriouts, MiscsImplement.MeritoriousHandle)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cDailyMeritorious, MiscsImplement.MeritoriousDailyHandle)

EventCallDispatcher.registerEventCall( aeDailyMeritorious, EventCallDispatcher.DailyMeritoriousChange )
